package com.example.appframework.login.register.create;

import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appframework.MainActivity;
import com.example.appframework.R;
import com.example.appframework.login.LoginMainActivity;
import com.example.appframework.mysql.DBConnection;

public class CreateFragment extends Fragment {

    private CreateViewModel mViewModel;
    EditText registerUsername, registerPassword, registerPassword2;
    ImageView imageUsername, imagePassword, imagePassword2;
    TextView logRPassword, logRPassword2;
    private String username, password, password2;

    public static CreateFragment newInstance() {
        return new CreateFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.r_create_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(CreateViewModel.class);
        // TODO: Use the ViewModel

        registerUsername = getView().findViewById(R.id.registerUsername);
        registerPassword = getView().findViewById(R.id.registerPassword);
        registerPassword2 = getView().findViewById(R.id.registerPassword2);

        imageUsername = getView().findViewById(R.id.imageUsername);
        imagePassword = getView().findViewById(R.id.imagePassword);
        imagePassword2 = getView().findViewById(R.id.imagePassword2);

        logRPassword = getView().findViewById(R.id.logRPassword);
        logRPassword2 = getView().findViewById(R.id.logRPassword2);

        imageUsername.setVisibility(View.INVISIBLE);
        imagePassword.setVisibility(View.INVISIBLE);
        imagePassword2.setVisibility(View.INVISIBLE);
        logRPassword.setVisibility(View.INVISIBLE);
        logRPassword2.setVisibility(View.INVISIBLE);

        setMyType(getActivity(), 1);

        registerUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imageUsername.setVisibility(View.VISIBLE);
                username = registerUsername.getText().toString();
                if(TextUtils.isEmpty(username)){
                    imageUsername.setImageResource(R.drawable.ic_baseline_error);
                }
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imageUsername.setVisibility(View.VISIBLE);
                username = registerUsername.getText().toString();
                if(TextUtils.isEmpty(username)){
                    imageUsername.setImageResource(R.drawable.ic_baseline_error);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                imageUsername.setVisibility(View.VISIBLE);
                username = registerUsername.getText().toString();
                if(TextUtils.isEmpty(username)){
                    imageUsername.setImageResource(R.drawable.ic_baseline_error);
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Boolean temp = true;
                            if(DBConnection.check_usename(username)){
                                temp = false;
                                Log.i("show", "been used!");
                            }
                            imageUsername.setImageResource(temp ? R.drawable.ic_baseline_right : R.drawable.ic_baseline_error);
                            if(TextUtils.isEmpty(username)){
                                imageUsername.setImageResource(R.drawable.ic_baseline_error);
                            }
                        }
                    }).start();
                }
            }
        });

        registerPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imagePassword.setVisibility(View.VISIBLE);
                password = registerPassword.getText().toString();
                if(TextUtils.isEmpty(password)){
                    imagePassword.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imagePassword.setVisibility(View.VISIBLE);
                password = registerPassword.getText().toString();
                if(TextUtils.isEmpty(password)){
                    imagePassword.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                imagePassword.setVisibility(View.VISIBLE);
                password = registerPassword.getText().toString();
                if(TextUtils.isEmpty(password)){
                    imagePassword.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword.setVisibility(View.VISIBLE);
                }
                else {
                    imagePassword.setImageResource(checkPassword(password) ? R.drawable.ic_baseline_right : R.drawable.ic_baseline_error);
                    logRPassword.setVisibility(checkPassword(password) ? View.INVISIBLE : View.VISIBLE);
                }
            }
        });

        registerPassword2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imagePassword2.setVisibility(View.VISIBLE);
                password2 = registerPassword2.getText().toString();
                if(TextUtils.isEmpty(password2)){
                    imagePassword2.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imagePassword2.setVisibility(View.VISIBLE);
                password2 = registerPassword2.getText().toString();
                if(TextUtils.isEmpty(password2)){
                    imagePassword2.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                imagePassword2.setVisibility(View.VISIBLE);
                password2 = registerPassword2.getText().toString();
                if(TextUtils.isEmpty(password2)){
                    imagePassword2.setImageResource(R.drawable.ic_baseline_error);
                    logRPassword2.setVisibility(View.VISIBLE);
                }
                else {
                    imagePassword2.setImageResource(checkPassword(password2)&&password2.equals(password) ? R.drawable.ic_baseline_right : R.drawable.ic_baseline_error);
                    logRPassword2.setVisibility(checkPassword(password2)&&password2.equals(password) ? View.INVISIBLE : View.VISIBLE);
                }
            }
        });

        getView().findViewById(R.id.buttonRegisterNext1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = registerUsername.getText().toString();
                password = registerPassword.getText().toString();
                password2 = registerPassword2.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)||TextUtils.isEmpty(password2)) {
                    Log.i("show", "empty!");
                }
                else if(!password.equals(password2)){
                    Log.i("show", "unequal!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.check_usename(username)){
                                Log.i("show", "been used!");
                            }
                            else{
                                Bundle bundle = new Bundle();
                                bundle.putString("username", username);
                                bundle.putString("password", password);
                                Log.i("show", "okk!");
                                NavController navController = Navigation.findNavController(getActivity(),R.id.fragmentLogin);
                                navController.navigate(R.id.action_createFragment_to_verificationFragment, bundle);
                            }
                        }
                    }).start();
                }
            }
        });
    }

    private Boolean checkPassword(String str){
        Boolean f0 = false, f1 = false, f2 = false, f3 = false, f4 = false;
        f0 = str.length() >= 6 && str.length() <= 18;
        for(int i=0; i < str.length(); i++){
            char c = str.charAt(i);
            if(Character.isDigit(c)){
                f1 = true;
            }
            else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                f2 = true;
            }
            else if((c>='!'&&c<='/')||(c>=':'&&c<='@')||(c>='['&&c<='`')||(c>='{'&&c<='~')){
                f3 = true;
            }
            else {
                f4 = true;
            }
        }
        return f0 && (f1 & f2 & f3) && !f4;
    }

    private void setMyType(Activity activity, int t) {
        super.onAttach(activity);
        ((LoginMainActivity) activity).setType(t);
    }

}